<?php
// +----------------------------------------------------------------------
// | Wendasns [ 开源问答社区系统 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2020~2021 http://wendasns.com All rights reserved.
// +----------------------------------------------------------------------
namespace app\wendasns\admin;
use app\admin\AdminBaseController;
use app\wendasns\model\User as byUser;
use app\wendasns\model\Member;
use app\wendasns\model\Group;
use app\wendasns\service\PwUser;
use app\wendasns\service\PwCredit;

class User extends AdminBaseController
{
	
    public function index()
    {
    	$dm = Group::select();
		return view('index',['group'=>$dm]);
    }

    public function list()
    {
    	$limit = input('limit',5,'intval');
    	$uid = input('uid',0,'intval');
    	$username = input('username','');
    	$email = input('email','');
    	$group = input('group',0,'intval');
    	$status = input('status','','intval');

    	if($uid){
    		$sql[] = ['id','=',$uid];
    	}else{
    		if($email){
    			$sql[] = ['email','=',$email];
    		}elseif($username){
    			$sql[] = ['username','like','%'.$username.'%'];
    		}else{
    			$sql = [];
    		}
    	}
    	
    	if($group){
    		$msql[] = ['group_id','=',$group];
    	}
    	if(strlen($status)>0){
    		$msql[] = ['status','=',$status];
    	}
    	
    	if(isset($msql)){
    		$from = Member::where($msql)->select();
    	}else{
    		$from = Member::select();
    	}

		$in = [];
		foreach($from as $v){
			$in[] = $v->user_id;
		}
		$sql[] = ['id','in',$in];
    		
    	$dm = byUser::where($sql)->order('create_time', 'desc')->paginate($limit);
    	
    	foreach($dm as $k=>$v){
    		$dm[$k]->group_id = isset($v->members->group_id)?$v->members->group_id:0;
    		$dm[$k]->status = isset($v->members->status)?$v->members->status:0;
    	}
    	$data = $dm->toArray();
		success('ok', $data['data'], ['count'=>$data['total']]);
    }

	//添加
	public function add()
    {
		$username = input('post.username','');
		$password = input('post.password','');
		$email = input('post.email','');
		$mobile = input('post.mobile','');
		$group_id = input('post.group',0,'intval');
		
    	try {
            validate(\app\ucentre\validate\Register::class)->check([
            	'username' => $username,
            	'password' => $password,
            	'repassword' => $password,
            	'email' => $email,
            	'mobile' => $mobile,
            ]);
        } catch (\think\exception\ValidateException $e) {
            // 验证失败 输出错误信息
            error($e->getError());
        }
        
		$dm = Group::find($group_id);
		if(!$dm){
			error('用户组不存在');
		}
		
        try{
        	$user = new PwUser();
        	$user->username = $username;
        	$user->password = $password;
        	$user->email = $email;
        	$user->mobile = $mobile;
        	$user->group = $group_id;
        	$user->save();
    	}catch (\think\Exception $e) {
    		error($e->getMessage());
    	}
    	
		success('操作成功');
    }
    
    //删除
    public function remove()
    {
		$id = input('post.id',[]);
		if(!is_array($id)){
			error('数据类型不正确');
		}
        try{
        	$user = new PwUser();
        	$user->uid = $id;
        	$user->delete();
    	}catch (\think\Exception $e) {
    		error($e->getMessage());
    	}
		success('操作成功');
    }
    
    //编辑
    public function edit()
    {
    	if(!request()->isPost()){
    		$dm = Group::select();
    		return view('edit',['group'=>$dm]);
    	}
    	$cm = input('post.cm','info');
		switch ($cm){
			case 'info':
				$this->_info();
				break;
			case 'credit':
				$this->_credit();
				break;
			default:
		}
    	success('操作成功');
    }

    private function _info()
    {
		$uid = input('post.uid',0,'intval');
		$email = input('post.email','');
		$mobile = input('post.mobile','');
		$password = input('post.password','');
		$repassword = input('post.repassword','');
		$group = input('post.group',0,'intval');
		$admingroup = input('post.admingroup',0,'intval');
		$status = input('post.status',0,'intval');
		$auths = input('post.auths',0,'intval');
		
    	try {
            validate([
    	    	'repassword' =>'confirm:password',
            	'email' => 'require|email',
            	'mobile' => 'mobile',
            ])->check([
            	'password' => $password,
            	'repassword' => $password,
            	'email' => $email,
            	'mobile' => $mobile,
            ]);
        } catch (\think\exception\ValidateException $e) {
            error($e->getError());
        }

        try{
        	$user = new PwUser();
        	$user->id = $uid;
        	$user->password = $password;
        	$user->email = $email;
        	$user->mobile = $mobile;
        	$user->group = $group;
        	$user->admin = $admingroup;
        	$user->status = $status;
        	$user->auths = $auths;
        	$user->update();
    	}catch (\think\Exception $e) {
    		error($e->getMessage());
    	}
    }
    
    private function _credit()
    {
    	$status = input('post.status',0,'intval');
        try{
        	$dm = new PwCredit();
        	$dm->credit1 = $val;
        	$dm->save();
    	}catch (\think\Exception $e) {
    		error($e->getMessage());
    	}
    }
    
    public function check()
    {
    	$id = input('post.id',[]);
    	$group = input('post.group','');
    	$status = input('post.status','');

		//$dm = byUser::select($id);
		$list = [];
		if(strlen($group)>0) $list['group_id'] = $group;
		if(strlen($status)>0) $list['status'] = $status;
		
		$dm = byUser::where('app','wendasns')->select($id);
		
		foreach($dm as $v){
			foreach($v->froms as $val){
				$val->save($list);
			}
			//$a[] = $v->froms;
			//$v->froms()->saveAll($list);
		}
		//halt($a);
		/*if(strlen($group)>0) $dm->group_id = $group;
		if(strlen($group)>0) $dm->status = $status;
		$dm->together(['froms'])->save();*/
    	success('操作成功');
    }
}
